<?php
session_start();
$page = 'Forum';

//Pagina in ontwikkeling
$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
$no_style = true;
include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
echo "<div class='information'><p class='icon'>Deze pagina is nog in ontwikkeling.</p></div>";
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
die();

//Ben je wel ingelogd?
if(empty($_SESSION['spelerId'])) {
	$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo "<div class='error'><p class='icon'>Login om deze pagina te bekijken.</p></div>";
	include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
	die();
}
/* //Heb je wel het recht om deze pagina te bekijken?
if(($_SESSION['rechten'] & (1 << 14)) == 0) {
	$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />";
	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo "<div class='error'><p class='icon'>Je hebt niet het recht om berichten toe te voegen.</p></div>";
	include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
	die();
}
*/

include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/database_connectie.php");
if(isset($_POST['toevoegen'])) {
	$error = "";
	
	if(empty($_POST['forumId'])) {
		$error .= "<div class='error'><p class='icon'>Er is geen onderwerp geselecteerd.</p></div>";
	}
	$bericht = htmlentities($_POST['bericht'], ENT_QUOTES);
	if(empty($_POST['bericht']) || empty($bericht)) {
		if(empty($_POST['bericht'])) {
			$error .= "<div class='error'><p class='icon'>Er is geen bericht ingevoerd.</p></div>";
		}else {
			$error .= "<div class='error'><p class='icon'>Er is iets fout gegaan tijdens het verwerken van het bericht.</p></div>";
		}
	}
	if(strlen($bericht) > 1000) {
		$error .=  "<div class='error'><p class='icon'>De lengte van het bericht (na verwerking) is te lang. (".strlen($bericht)."/1000).</p></div>";
	}
	
	if($error == "") {
		$result = mysql_query("INSERT INTO Forumbericht (forumId, geschrevenDoor, geschrevenOp, bericht)
								VALUES('".$_POST['forumId']."', '".$_SESSION['spelerId']."', NOW(), '".$bericht."')");
		if($result) {
			//header("Location: http://localhost/forum/?forumId=".$_POST['forumId'])
			$bestanden = "<script type='text/javascript' src='/bestanden/js/post/js_post.js'></script>
						<script type='text/javascript'>window.onload = function() { post_to_url(\"./?forumId=".$_POST['forumId']."\", {})};</script>";;
		}else {
			$error .= "<div class='error'><p class='icon'>Er is iets mis gegaan tijdens het versturen van het bericht. Het bericht is niet toegevoegd.</p></div>";
		}
	}
}

if(isset($_POST['toevoegen']) && $error == "") {
	include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/quick_post.php");
	die();
}else {
	$style_nmr = (isset($_SESSION['style']) && $_SESSION['style'] > 1 ? $_SESSION['style'] : "");
	$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/css/style_tabellen".$style_nmr.".css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/style_in_popup".$style_nmr.".css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/write_bbcode.css' />
	<link rel='stylesheet' type='text/css' href='/bestanden/js/bbcode/css/read_bbcode.css' />
	<script type='text/javascript' src='/bestanden/js/bbcode/js/bbcode.js'></script>";

	$no_style = true;
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	echo $error;
	
	echo "<form action='reactieAanmaken.php' method='post'>";
		echo "<input type='hidden' name='forumId' value='".(isset($_GET['id']) ? $_GET['id'] : $_POST['forumId'])."'>";
		echo "<input type='hidden' name='teamId' value='3'>";
		
		echo "<div class='button_bar'>";
			echo "<div class='bbcode_buttons'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/bold.gif' onclick='insert_bbcode(\"[b]\", \"[/b]\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/italic.gif' onclick='insert_bbcode(\"[i]\", \"[/i]\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/underline.gif' onclick='insert_bbcode(\"[u]\", \"[/u]\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/ordered.gif' onclick='insert_bbcode(\"[list=1][*]\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/unordered.gif' onclick='insert_bbcode(\"[list][*]\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/link.gif' onclick='insert_prompt_bbcode(\"[url]\", \"[/url]\", \"bericht\", \"Geef de URL op\", \"http://\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/image.gif' onclick='insert_prompt_bbcode(\"[img]\", \"[/img]\", \"bericht\", \"Geef de locatie van het plaatje op\", \"http://\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/quote.gif' onclick='insert_bbcode(\"[quote]\", \"[/quote]\", \"bericht\");'>";
				echo "<img class='bbcode_button' src='/images/style/buttons/bbcode/code.gif' onclick='insert_bbcode(\"[code]\", \"[/code]\", \"bericht\");'>";
			echo "</div>";
			echo "<div class='bbcode_emoticons'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/bigsmile.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/smile.gif' onclick='insert_bbcode(\":)\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/laugh.gif' onclick='insert_bbcode(\"XD\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/tongue.gif' onclick='insert_bbcode(\":P\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/wink.gif' onclick='insert_bbcode(\":D\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/frown.gif' onclick='insert_bbcode(\":(\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/surprise.gif' onclick='insert_bbcode(\":O\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/worry.gif' onclick='insert_bbcode(\":s\", \"\", \"bericht\");'>";
				echo "<img class='bbcode_emoticon' src='/images/style/emoticons/sweatdrop.gif' onclick='insert_bbcode(\"^^\", \"\", \"bericht\");'>";    
			echo "</div>";
		echo "</div>";
		echo "<div class='clr'></div>";
		
		
		//quote
		if(isset($_GET['quote'])) {
			$result = mysql_query("SELECT Forumbericht.bericht, Speler.voornaam, Speler.tussenvoegsel, Speler.achternaam
				FROM Forumbericht, Speler
				WHERE Forumbericht.forumberichtId = '".$_GET['quote']."'
				AND Speler.spelerId = Forumbericht.geschrevenDoor");
			if($result <> null && mysql_num_rows($result) > 0) {
				$row = mysql_fetch_object($result);
				$bericht = "[quote=".$row->voornaam." ".(isset($row->tussenvoegsel) ? $row->tussenvoegsel." " : "").$row->achternaam."]".$row->bericht."[/quote]";
			}
		}
		echo "<div class='textarea'>";
			echo "<textarea name='bericht' id='bericht' maxlength='1000'>".(isset($_POST['bericht']) ? $_POST['bericht'] : $bericht)."</textarea>";
		echo "</div>";
		echo "<span class='button'><input type='submit' name='voorbeeld' value='Voorbeeld'></span> ";
		echo "<span class='button'><input type='submit' name='toevoegen' value='Verder'></span>";
	echo "</form>";
	
	if(isset($_POST['voorbeeld'])) {
		include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/nbbc_parser/initialize_nbbc.php");
		
		echo "<div class='voorbeeld'>";
			echo "<p>Voorbeeld<p>";
			echo "<div class='entry'>";
				echo $bbcode->Parse($_POST['bericht']);
			echo "</div>";
		echo "</div>";
	}
}
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
?>